//
//  GroupInfoCtr-ext.swift
//  eemall
//
//  Created by zhuxietong on 2018/12/29.
//  Copyright © 2018 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit


extension GroupInfoCtr{
    class EndTimerNode: MoDisplayNode {
        var dayTag = ASButtonNode()
        var hoursTag = ASButtonNode()
        var minTag = ASButtonNode()
        var secondTag = ASButtonNode()
        var endTime:Date = Date(){
            didSet{
                updateTime()
                startTimer()
            }
        }
        
        
        var endTimeNotice:()->Void = {}
        var timeOutTag = ASButtonNode()
        
        
        func timeNum(num:Int) -> String {
            var str = "\(num)"
            if str.len == 1{
                str = "0\(str)"
            }
            return str
        }
        
        var compnents:(day:Int,hour:Int,minu:Int,sec:Int) = (0,0,0,0){
            didSet{
                
                dayTag.ui.ntitle(title: "\(timeNum(num: compnents.day))")
                hoursTag.ui.ntitle(title: "\(timeNum(num: compnents.hour))")
                minTag.ui.ntitle(title: "\(timeNum(num: compnents.minu))")
                secondTag.ui.ntitle(title: "\(timeNum(num: compnents.sec))")
                
            }
        }
        override func addLayoutRules() {
            //            assignStyle(button: timeOutTag)
            assignStyle(button: dayTag)
            assignStyle(button: hoursTag)
            assignStyle(button: minTag)
            assignStyle(button: secondTag)
        }
        
        var timer:SwiftTimer? = nil

    
        func startTimer(interval:Int = 1) {
            //            self.updateTime()
            weak var wself = self
            timer?.suspend()
            self.timer = SwiftTimer(interval: .seconds(interval), repeats: true, queue: .main, handler: { (timer) in
                wself?.updateTime()
            })
            timer?.start()
        }
        
      
        
        func stopTimer() {
            timer?.suspend()
        }
        
        @objc open func updateTime() {
//            let end = timeStringToDate("2018-12-29 10:28:20.000")
            let info = endTime.timeSinceComponens()
            switch info.0 {
            case .later:
                compnents = info.1
                isTimeout = false
                setNeedsLayout()
                setNeedsDisplay()
            case .equal:
                endTimeNotice()
                compnents = (0,0,0,0)
                isTimeout = true
                setNeedsLayout()
                setNeedsDisplay()
            default:
                isTimeout = true
                compnents = (0,0,0,0)
//                isTimeout = true
                setNeedsLayout()
                setNeedsDisplay()
            }
        }
        
        @discardableResult
        open func assignStyle(button:ASButtonNode)->ASButtonStyle{
            button.bsui.set(radius: 2, width: nil, color: nil)
            return button.ui.font(size: 13).padding(insets: [1,1]).ncolor(hex: "#fff").buttonColor(hex: AppTheme.main_hex)
        }
        
        var isTimeout:Bool = false{
            didSet{
                if oldValue != isTimeout{
                    let timeStr = Date()["yyyy年"]
                    let endStr = endTime["yyyy年MM月dd日 HH:mm"]
                    let str = endStr.replacingOccurrences(of: timeStr, with: "")
                    timeOutTag.ui.font(size: 13).padding(insets: [1,4]).ncolor(hex: "#fff").buttonColor(hex: "#888").ntitle(title:str).set(radius: 2, width: nil, color: nil)
//                    observerStatus(isTimeout)
                }
                if isTimeout{
                    stopTimer()
                }
            }
        }
        
        var observerStatus:(_:Bool)->Void = {_ in}
        override func specGetter() -> _Spec {
            if isTimeout{
                return _Spec.stack(["h|6",&-.center], childs: [
                    timeOutTag.Spec
                    ])
            }
            var items:[_Spec] = [_Spec]()
            if compnents.day > 0{
                items.append(dayTag.Spec)
            }
            items = items + [hoursTag[24],minTag[24],secondTag[24]]
            return _Spec.stack(["h|6",&-.center], childs: items)
        }
    }
    
    class EndTimerNode1: EndTimerNode {
        open override func assignStyle(button:ASButtonNode)->ASButtonStyle{
            button.bsui.set(radius: 2, width: nil, color: nil)
            return button.ui.font(size: 11).padding(insets: [1,0]).ncolor(hex: "#fff").buttonColor(hex: "\(AppTheme.main_hex)88")
        }
    }

}
